﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using THSERP.BusinessEntities;
using THSLib;

namespace THSERP.BaseProvider
{
    public partial class guiCopy : THSERPScreen
    {
        DataTable tblCopiedFields = new DataTable();

        public guiCopy()
        {
            InitializeComponent();
        }

        public guiCopy(BaseModuleERP mdl)
        {
            InitializeComponent();
            Module = mdl;
        }

        #region Utility Functions
        public void InitProfileObjectsGrid()
        {
            
            String strMainTableName = THSUtil.GetTableNameFromBusinessObject(((BaseModuleERP)Module).CurrentModuleEntity.MainObject);
            BaseBusinessController objBusinessController = BusinessControllerFactory.GetBusinessController(strMainTableName + "Controller");

            DataSet dsProfiles = objBusinessController.GetAllProfileObjects();
            if (dsProfiles != null)
            {
                String strColumnNumber = THSDbUtil.GetTablePrimaryColumn(strMainTableName);
                String strColumnName = strColumnNumber.Substring(0, strColumnNumber.Length - 2) + "Name";
                colObjectNumber.FieldName = strColumnNumber;
                colObjectName.FieldName = strColumnName;

                fld_dgcProfileObjects.DataSource = dsProfiles.Tables[0];                
            }
        }

        public void InitCopiedFieldsDataTableStructure()
        {
            tblCopiedFields = new DataTable();

            DataColumn colFieldName = new DataColumn();
            colFieldName.DataType = typeof(String);
            colFieldName.ColumnName = "FieldName";
            tblCopiedFields.Columns.Add(colFieldName);

            DataColumn colFieldCaption = new DataColumn();
            colFieldCaption.DataType = typeof(String);
            colFieldCaption.ColumnName = "FieldCaption";
            tblCopiedFields.Columns.Add(colFieldCaption);

            DataColumn colFieldType = new DataColumn();
            colFieldType.DataType = typeof(String);
            colFieldType.ColumnName = "FieldType";
            tblCopiedFields.Columns.Add(colFieldType);

            DataColumn colFieldValue = new DataColumn();
            colFieldValue.DataType = typeof(String);
            colFieldValue.ColumnName = "FieldValue";
            tblCopiedFields.Columns.Add(colFieldValue);
        }

        public void InitCopiedFieldsDataTable(int iObjectID)
        {
            
            InitCopiedFieldsDataTableStructure();

            
            String strMainTableName = THSUtil.GetTableNameFromBusinessObject(((BaseModuleERP)Module).CurrentModuleEntity.MainObject);
            BusinessObject objMainObjectInfo = BusinessObjectFactory.GetBusinessObject(strMainTableName + "Info");
            BaseBusinessController objMainObjectController = BusinessControllerFactory.GetBusinessController(strMainTableName + "Controller");
            objMainObjectInfo = (BusinessObject)objMainObjectController.GetObjectByID(iObjectID);

            foreach (PropertyInfo prop in objMainObjectInfo.GetType().GetProperties())
            {
                String strPropertyName = prop.Name;
                String strFieldCaption = strPropertyName;
                AAColumnAliasInfo objAAColumnAliasInfo = new AAColumnAliasController().GetAAColumnAliasByColumnNameAndTableName(strPropertyName,strMainTableName);
                if (objAAColumnAliasInfo != null)
                    strFieldCaption = objAAColumnAliasInfo.AAColumnAliasCaption;

                object objValue = prop.GetValue(objMainObjectInfo, null);
                if (objValue != null)
                {
                    if (!THSDbUtil.IsPrimaryKey(strMainTableName, strPropertyName))
                    {
                        if (THSDbUtil.IsForeignKey(strMainTableName, strPropertyName))
                        {
                            if (Convert.ToInt32(objValue) > 0)
                            {
                                tblCopiedFields.Rows.Add(new object[] { prop.Name,strFieldCaption, prop.PropertyType.Name, objValue.ToString() });
                            }
                        }
                        else
                        {
                            if (!prop.Name.StartsWith("AA"))
                            {
                                if (prop.PropertyType == typeof(String))
                                {
                                    if (objValue.ToString().StartsWith("C"))
                                    {
                                        tblCopiedFields.Rows.Add(new object[] { prop.Name, strFieldCaption, prop.PropertyType.Name, objValue.ToString() });
                                    }
                                }
                                else if (prop.PropertyType == typeof(int))
                                {
                                    if (Convert.ToInt32(objValue) != 0)
                                    {
                                        tblCopiedFields.Rows.Add(new object[] { prop.Name, strFieldCaption, prop.PropertyType.Name, Convert.ToInt32(objValue) });
                                    }
                                }
                                else if (prop.PropertyType == typeof(double))
                                {
                                    if (Convert.ToDouble(objValue) != 0)
                                    {
                                        tblCopiedFields.Rows.Add(new object[] { prop.Name, strFieldCaption, prop.PropertyType.Name, Convert.ToDouble(objValue) });
                                    }
                                }
                                else if (prop.PropertyType == typeof(bool))
                                {
                                    if (!prop.Name.Equals("AllowPropertyChangedEvent"))
                                    {
                                        if (Convert.ToBoolean(objValue) == true)
                                        {
                                            tblCopiedFields.Rows.Add(new object[] { prop.Name, strFieldCaption, prop.PropertyType.Name, Convert.ToBoolean(objValue) });
                                        }
                                    }
                                }
                            }                            
                        }
                    }
                }

            }
            
        }

        public void CopyObjectsByProfile()
        {
            

            String strMainTableName = THSUtil.GetTableNameFromBusinessObject(((BaseModuleERP)Module).CurrentModuleEntity.MainObject);
            String strPrimayColumn = THSDbUtil.GetTablePrimaryColumn(strMainTableName);
            BaseBusinessController objMainObjectController = BusinessControllerFactory.GetBusinessController(strMainTableName + "Controller");

            THSProgressBar.Start("Kopie");

            foreach (DataRow row in Module.Toolbar.ObjectCollection.Tables[0].Rows)
            {                
                BusinessObject objInfo = (BusinessObject)objMainObjectController.GetObjectFromDataRow(row);

                int iObjectID = Convert.ToInt32(THSDbUtil.GetPropertyValue(objInfo, strPrimayColumn));
                if (iObjectID != Module.Toolbar.CurrentObjectID)
                {
                    THSProgressBar.SetTHSText(iObjectID.ToString());

                    foreach (DataRow rowCopiedFields in tblCopiedFields.Rows)
                    {
                        String strFieldName = rowCopiedFields["FieldName"].ToString();
                        String strFieldCaption = rowCopiedFields["FieldCaption"].ToString();
                        String strFieldType = rowCopiedFields["FieldType"].ToString();
                        String strFieldValue = rowCopiedFields["FieldValue"].ToString();

                        if (strFieldType.Equals("String"))
                        {                            
                            object objValue = THSDbUtil.GetPropertyValue(((BaseModuleERP)Module).CurrentModuleEntity.MainObject, strFieldName);
                            THSDbUtil.SetPropertyValue(objInfo, strFieldName, objValue);
                        }
                        else if (strFieldType.Equals("Boolean"))
                        {
                            object objValue = THSDbUtil.GetPropertyValue(((BaseModuleERP)Module).CurrentModuleEntity.MainObject, strFieldName);
                            THSDbUtil.SetPropertyValue(objInfo, strFieldName, objValue);
                        }
                        else if (strFieldType.Equals("Int32"))
                        {                            
                            object objValue = THSDbUtil.GetPropertyValue(((BaseModuleERP)Module).CurrentModuleEntity.MainObject, strFieldName);
                            THSDbUtil.SetPropertyValue(objInfo, strFieldName, objValue);                                                       
                        }
                        else if (strFieldType.Equals("Double"))
                        {
                            double dbValue = Convert.ToDouble(strFieldValue);
                            if (dbValue == 1)
                            {
                                object objValue = THSDbUtil.GetPropertyValue(((BaseModuleERP)Module).CurrentModuleEntity.MainObject, strFieldName);
                                THSDbUtil.SetPropertyValue(objInfo, strFieldName, objValue);
                            }
                            else
                            {
                                object objPropertyValue = THSDbUtil.GetPropertyValue(objInfo, strFieldName);
                                if(objPropertyValue!=null)
                                    THSDbUtil.SetPropertyValue(objInfo, strFieldName, Convert.ToDouble(objPropertyValue) * dbValue / 100);
                            }
                        }                        
                    }

                    objMainObjectController.UpdateObject(objInfo);
                }                
            }

            THSProgressBar.Close();
            MessageBox.Show("Kết thúc", "#Cảnh báo#", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        #endregion

        private void guiCopy_Load(object sender, EventArgs e)
        {
            InitProfileObjectsGrid();
        }

        private void fld_dgvProfileObjects_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            int iObjectID = Convert.ToInt32(fld_dgvProfileObjects.GetRowCellValue(e.FocusedRowHandle, colObjectNumber));

            if (iObjectID > 0)
            {
                InitCopiedFieldsDataTable(iObjectID);
                fld_dgcCopiedFields.DataSource = tblCopiedFields;
            }
        }

        private void fld_btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void fld_btnCopy_Click(object sender, EventArgs e)
        {
            CopyObjectsByProfile();
        }
    }
}